package com.dev.op.consumer;

import com.dev.op.VO.SubmissionVO;
import com.dev.op.entity.Submission;
import com.dev.op.service.SubmissionService;
import com.dev.op.util.RedisUtil;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Duration;

@Component
public class JudgeTaskConsumer {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SubmissionService submissionService;

    @PostConstruct
    public void startConsumerThread() {
        new Thread(() -> {
            while (true) {
                try {
                    SubmissionVO submissionVO = redisUtil.blockingPopJudgeTask(Duration.ofSeconds(0));
                    if (submissionVO != null) {
                        submissionService.addSubmission(submissionVO);
                    }
                } catch (Exception e) {
                    System.err.println("判题线程异常：" + e.getMessage());
                    e.printStackTrace();
                }
            }
        }, "judge-consumer-thread").start();
    }
}
